package com.ruoyi.phoneController;

import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.domain.MobilServiceDetails;
import com.ruoyi.mapper.MobilServiceDetailsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 手机端服务Controller
 *
 * @author ruoyi
 * @date 2025-01-20
 */
@RestController
@RequestMapping("/mobile/service")
public class MobilServiceController extends BaseController {
    @Autowired
    private MobilServiceDetailsMapper mobilServiceDetailsMapper;

    @Autowired
    private TokenService tokenService;
    /**
     * 根据商户ID查询服务列表
     */
    @GetMapping("/merchant/{merchantId}")
    public AjaxResult getServicesByMerchant(@PathVariable Integer merchantId) {
        List<MobilServiceDetails> services = mobilServiceDetailsMapper.selectServicesByMerchantId(merchantId);
        return success(services);
    }

    /**
     * 根据服务类型查询服务列表
     */
    @GetMapping("/type/{typeId}")
    public AjaxResult getServicesByType(@PathVariable Integer typeId) {
        List<MobilServiceDetails> services = mobilServiceDetailsMapper.selectServicesByTypeId(typeId);
        return success(services);
    }

    /**
     * 根据ID查询服务详情
     */
    @GetMapping("/{id}")
    public AjaxResult getServiceDetail(@PathVariable Integer id) {
        MobilServiceDetails service = mobilServiceDetailsMapper.selectMobilServiceDetailsById(id);
        if (service == null) {
            return error("服务不存在");
        }
        return success(service);
    }

    /**
     * 查询服务列表（支持搜索）
     */
    @GetMapping("/search")
    public AjaxResult searchServices(
            @RequestParam(required = false) String name,
            @RequestParam(required = false) Integer merchantId,
            @RequestParam(required = false) Integer typeId) {
        
        MobilServiceDetails query = new MobilServiceDetails();
        query.setName(name);
        query.setmId(merchantId);
        query.setTypeId(typeId);
        query.setDeleted(0);
        
        List<MobilServiceDetails> services = mobilServiceDetailsMapper.selectMobilServiceDetailsList(query);
        return success(services);
    }

    /**
     * 获取热门服务（可以根据订单数量等指标）
     */
    @GetMapping("/popular")
    public AjaxResult getPopularServices(@RequestParam(defaultValue = "10") Integer limit) {
        // 这里可以根据实际业务逻辑来获取热门服务
        // 目前先返回所有服务，后续可以优化
        MobilServiceDetails query = new MobilServiceDetails();
        query.setDeleted(0);
        
        List<MobilServiceDetails> services = mobilServiceDetailsMapper.selectMobilServiceDetailsList(query);
        
        // 限制返回数量
        if (services.size() > limit) {
            services = services.subList(0, limit);
        }
        
        return success(services);
    }

    /**
     * 根据价格区间查询服务
     */
    @GetMapping("/price-range")
    public AjaxResult getServicesByPriceRange(
            @RequestParam(required = false) Float minPrice,
            @RequestParam(required = false) Float maxPrice,
            @RequestParam(required = false) Integer typeId) {
        
        // 这里需要在Mapper中添加对应的方法来支持价格区间查询
        // 目前先返回基本查询结果
        MobilServiceDetails query = new MobilServiceDetails();
        query.setTypeId(typeId);
        query.setDeleted(0);
        
        List<MobilServiceDetails> services = mobilServiceDetailsMapper.selectMobilServiceDetailsList(query);
        
        // 在代码中过滤价格区间（后续可以优化到SQL中）
        if (minPrice != null || maxPrice != null) {
            services = services.stream()
                    .filter(service -> {
                        Float price = service.getPresentPrice();
                        if (price == null) return false;
                        
                        boolean matchMin = minPrice == null || price >= minPrice;
                        boolean matchMax = maxPrice == null || price <= maxPrice;
                        
                        return matchMin && matchMax;
                    })
                    .collect(java.util.stream.Collectors.toList());
        }
        
        return success(services);
    }
}











